package com.gxuwz.medical.database;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
 
public class DbUtil1 {
	static Connection conn = null;
	PreparedStatement pstmt = null;
	ResultSet rs = null;
 
	/**
	 * 得到数据库连接
	 */
	public static  Connection getConnection()  {
		// 通过Config获取数据库配置信息
		String driver = "com.mysql.jdbc.Driver";
		String url = "jdbc:mysql://127.0.0.1:3306/medcialdb?useUnicode=true&characterEncoding=utf8&useSSL=false";
		String user ="root";
		String pwd = "123456";
		try {
			// 指定驱动程序
			Class.forName(driver);
			// 建立数据库连结
			conn = DriverManager.getConnection(url, user, pwd);
		} catch (Exception e) {
			// 如果连接过程出现异常，抛出异常信息
			//throw new SQLException("驱动错误或连接失败！");
			return null;
		}
		return conn;
		
	}
 
	/**
	 * 释放资源
	 */
	public void closeAll() {
		// 如果rs不空，关闭rs
		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		// 如果pstmt不空，关闭pstmt
		if (pstmt != null) {
			try {
				pstmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		// 如果conn不空，关闭conn
		if (conn != null) {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
 
	/**
	 * 执行SQL语句，可以进行查询
	 */
	public ResultSet executeQuery(String preparedSql, Object[] param) {
		// 处理SQL,执行SQL
		try {
			// 得到PreparedStatement对象
			pstmt = conn.prepareStatement(preparedSql);
			System.out.println("连接数据库成功");
			if (param != null) {
				for (int i = 0; i < param.length; i++) {
					// 为预编译sql设置参数
					pstmt.setObject(i + 1, param[i]);
				}
			}
			// 执行SQL语句
			rs = pstmt.executeQuery();
		} catch (SQLException e) {
			// 处理SQLException异常
			e.printStackTrace();
		}
		return rs;
	}
 
	/**
	 * 执行SQL语句，可以进行增、删、改的操作，不能执行查询
	 * @throws SQLException 
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 * @throws ClassNotFoundException 
	 */
	public int executeUpdate(String preparedSql, Object[] param) {
 
		int num = 0;
		// 处理SQL,执行SQL
		try {
			this.getConnection();
			// 得到PreparedStatement对象
			pstmt = conn.prepareStatement(preparedSql);
			if (param != null) {
				for (int i = 0; i < param.length; i++) {
					// 为预编译sql设置参数
					pstmt.setObject(i + 1, param[i]);
				}
			}
			// 执行SQL语句
			num = pstmt.executeUpdate();
		} catch (SQLException e) {
			// 处理SQLException异常
			e.printStackTrace();
		}finally {
			this.closeAll();
		}
		return num;
	}
	public static void main(String[] args) {
		DbUtil1 dbUtil = new DbUtil1();
		System.out.println(dbUtil.getConnection());
	}


}
